﻿@attribute [TabItemOption(Text = "菜单管理")]
@using System.Diagnostics.CodeAnalysis;
@using BootstrapBlazorApp.Shared.Data.SYS;
@inject MessageService msg


@inject ICacheManager CacheManager

<div>
    <Table TItem="MenuDto" IsTree="true" AutoGenerateColumns="true" 
            IsStriped="true" IsBordered="true" IsMultipleSelect="true"
           ShowToolbar="true" ShowExtendButtons="false" ShowLoading="true"
           @bind-SelectedRows="SelectedItems" ShowAddButton="false" ShowEditButton="false" ShowDeleteButton="false"
           TreeNodeConverter="@TreeNodeConverter" OnTreeExpand="@OnTreeExpand" OnQueryAsync="@OnQueryAsync" >
        <TableColumns>
                <TableColumn CssClass="col-sm-6" @bind-Field="@context.AppCode" ComponentType="typeof(Select<string>)" Items="AppCode" Lookup="AppCode" />
                <TableColumn  CssClass="col-sm-6" @bind-Field="@context.MenuType" ComponentType="typeof(Select<string>)" Items="MenuType" Lookup="MenuType" />
        </TableColumns>
    </Table>
</div>

@code {
    private IEnumerable<SelectedItem> AppCode { get; set; }

    private IEnumerable<SelectedItem> MenuType { get; set; }


    [Parameter]
    [NotNull]
    public List<MenuDto>? SelectedItems{ get; set; }


    /// <summary>
    /// OnInitialized 方法
    /// </summary>
    protected async override void OnInitialized()
    {
        base.OnInitialized();

        // 模拟数据从数据库中获得
        AppCode = Request.System.QueryDictionaryByTypeAsync("AppCode").Result.data;
        MenuType = Request.System.QueryDictionaryByTypeAsync("MenuType").Result.data;

        var data = await Request.System.QueryMenuAsync();
    }

    /// <summary>
    /// 查询
    /// </summary>
    /// <param name="_"></param>
    /// <returns></returns>
    private async Task<QueryData<MenuDto>> OnQueryAsync(QueryPageOptions _)
    {
        var response = await Request.System.QueryMenuAsync();

        var data = new QueryData<MenuDto>()
            {
                Items = response.data
            };
        return data;
    }

    private Task<IEnumerable<TableTreeNode<MenuDto>>> TreeNodeConverter(IEnumerable<MenuDto> items)
    {
        // 构造树状数据结构
        var ret = BuildTreeNodes(items, null);
        return Task.FromResult(ret);

        IEnumerable<TableTreeNode<MenuDto>> BuildTreeNodes(IEnumerable<MenuDto> items, string? parentCode)
        {
            var ret = new List<TableTreeNode<MenuDto>>();
            var menuDtos = items.Where(i => i.ParentCode == parentCode);
            foreach (var item in menuDtos)
            {
                var temp = new TableTreeNode<MenuDto>(item);
                temp.HasChildren = items.Any(i => i.ParentCode == item.MenuCode);
                //temp.IsExpand = temp.HasChildren;
                temp.Items = BuildTreeNodes(items, item.MenuCode);
                ret.Add(temp);

            }
            return ret;
        }
    }

    private async Task<IEnumerable<TableTreeNode<MenuDto>>> OnTreeExpand(MenuDto foo)
    {
        var ret = new List<TableTreeNode<MenuDto>>();
        return ret;
    }

}
